﻿using DongFangGuoKai.WMSPlatform.Core;
using DongFangGuoKai.WMSPlatform.Core.Repository;
using DongFangGuoKai.WMSPlatform.Domain.Model;
using DongFangGuoKai.WMSPlatform.Domain.Models;

namespace DongFangGuoKai.WMSPlatform.Service.QueceApp
{
    public partial class QueceService : IQueceService
    {


        public readonly SimpleRepositroy<t_QueceRecard> _repoQueceRecard;


        public QueceService(SimpleRepositroy<t_QueceRecard> repoQueceRecard,
            IRepository<TShippingOrderDetail> shippingOrderDetailRepo,
            IRepository<TShippingLockCell> shippingLockCellRepo,
            WMSPlatformContext context
        )
        {
            _repoQueceRecard = repoQueceRecard;
            ShippingOrderDetailRepo = shippingOrderDetailRepo;
            ShippingLockCellRepo = shippingLockCellRepo;
            Context = context;
        }

        public IRepository<TShippingOrderDetail> ShippingOrderDetailRepo { get; }
        public IRepository<TShippingLockCell> ShippingLockCellRepo { get; }

        public WMSPlatformContext Context { get; }

        public bool InsertQuece(out string msg, t_QueceRecard item)
        {

            msg = "";

            _repoQueceRecard.Insert(item);
            return true;
        }



        /// <summary>
        /// 委托单验证
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="serialId">业务类型 </param>
        /// <param name="barCode">码体信息---委托单号</param>
        /// <returns></returns>
        public bool CommissionformVerify(out string msg, string serialId, string barCode)
        {
            msg = string.Empty;
            bool result = false;



            //判断业务类型
            if (serialId == "1")  //取样
            {
                //不去验证当前委托单是否排队，是否其他人占用了这个号
                var quece = _repoQueceRecard.GetFirst(t => t.BillNO == barCode && t.Status <= 1);
                if (quece != null)
                {
                    msg = "请勿重复排队！";
                    return false;
                }


                var materialIds = Context.TReceivingBillDetails.Where(t => t.BillNo == barCode)
                           .Select(it => it.MaterialId)
                           .ToList();
                if (materialIds == null || materialIds.Count == 0)
                {

                    msg = "确认是否下发入库单！";
                    return false;
                }
                result = Context.TInventories.Any(t => materialIds.Contains(t.MaterialId));
                if (!result)
                {

                    msg = "确认库存是由已被取出！";
                    return false;
                }



            }
            else if (serialId == "2")
            {

            }

            return true;
        }

        public bool QueceVerify(out string msg, string barCode)
        {
            msg = string.Empty;


            var quece = _repoQueceRecard.AsQueryable()
                .Where(t => t.BillNO == barCode)
                .OrderByDescending(o => o.Id)
                .First()
                ;


            if (quece == null)
            {
                return true;  
            }
            if (quece.Status == 0 || quece.Status == 1)
            {

                msg = "已生成排队信息";
                return false;  
            }

            return true;
        }


    }
}
